package com.jltx.xq.simplewatch.news.ui;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jltx.xq.simplewatch.R;
import com.jltx.xq.simplewatch.base.BaseSupportFragment;
import com.jltx.xq.simplewatch.news.adapter.WeChatNewsAdapter;
import com.jltx.xq.simplewatch.news.model.bean.WeChatNews;
import com.jltx.xq.simplewatch.news.presenter.IWeChatPresenter;
import com.jltx.xq.simplewatch.news.presenter.impl.WeChatPresenterImpl;
import com.jltx.xq.simplewatch.news.view.IWeChatNewsView;
import com.jltx.xq.simplewatch.utils.ListUtils;
import com.jltx.xq.simplewatch.utils.NetWorkUtil;
import com.jltx.xq.simplewatch.widget.BaseToolbar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.jltx.utils.XqLog;
import cn.jltx.utils.XqToast;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/08/20.
 */

public class WeChatNewsFragment extends BaseSupportFragment<IWeChatPresenter> implements IWeChatNewsView,SwipeRefreshLayout.OnRefreshListener,BaseQuickAdapter.RequestLoadMoreListener{
    private static final String TAG = "MyWeChatNewsFragment";
    private static final String CHANNEL_CODE_PARAM = "channel_code_prams";

    @BindView(R.id.toolbar_frg_home)
    BaseToolbar toolbar;
    @BindView(R.id.swipe_refresh_frg_wechat)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.recyclerview_frg_wechat)
    RecyclerView recyclerView;

    View emptyView;
    private String channel;
    private WeChatNewsAdapter weChatNewsAdapter;
    private List<WeChatNews> newsList = new ArrayList<>();

    public static WeChatNewsFragment newInstance(String channel) {
        WeChatNewsFragment fragment = new WeChatNewsFragment();
        Bundle bundle = new Bundle();
        bundle.putString(CHANNEL_CODE_PARAM,channel);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.fragment_wechat_news_act;
    }

    @Override
    public IWeChatPresenter initPresenter() {
        return new WeChatPresenterImpl(this);
    }

    @Override
    public void initView(View rootView, Bundle savedInstanceState) {
        channel = getArguments().getString(CHANNEL_CODE_PARAM);
        setSwipeRefreshLayoutColor(swipeRefreshLayout);
        recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
        recyclerView.setHasFixedSize(true);
        emptyView = getActivity().getLayoutInflater().inflate(R.layout.page_net_error, (ViewGroup) recyclerView.getParent(), false);
        emptyView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //刷新
                onRefresh();
            }
        });
    }

    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);
        weChatNewsAdapter = new WeChatNewsAdapter(newsList);
        weChatNewsAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
        weChatNewsAdapter.setEnableLoadMore(true);
        weChatNewsAdapter.setOnLoadMoreListener(this, recyclerView);
        recyclerView.setAdapter(weChatNewsAdapter);
        swipeRefreshLayout.setOnRefreshListener(this);
        toolbar.setTitle(R.string.menu_nav_wechat_news_act_main);
        toolbar.setOnDoubleClickListener(onDoubleClickListener);
        presenter.getWeChatNews(channel,true);
    }

    private BaseToolbar.OnDoubleClickListener onDoubleClickListener = new BaseToolbar.OnDoubleClickListener() {
        @Override
        public void onDoubleClick(View view, MotionEvent e) {
            recyclerView.scrollToPosition(0); //双击快速回滚到第一项
        }
    };


    @Override
    public void onRefresh() {
        //下拉取网络获取数据
        presenter.getWeChatNews(channel,true);
    }

    @Override
    public void onLoadMoreRequested() {
        if (presenter.getLastNewsRecordPage() == 0 || presenter.getLastNewsRecordPage() == 1) {
            //如果记录的页数为0(即是创建的空记录)，或者页数为1(即已经是第一条记录了)
            //mRefreshLayout.endLoadingMore();//结束加载更多
            weChatNewsAdapter.loadMoreEnd();
            return;
        }
        //上拉去本地数据库或者上一页获取数据
        presenter.getWeChatNews(channel,false);
    }

    @Override
    public void onGetNewsListSuccess(List<WeChatNews> weChatNewsList, boolean isDown) {
        if(weChatNewsList != null){
            XqLog.d(TAG,"--------------------onGetNewsListSuccess");
            for (WeChatNews weChatNews : weChatNewsList){
                XqLog.d(TAG,"title=>"+weChatNews.getTitle());
            }
            XqLog.d(TAG,"onGetNewsListSuccess--------------------");
            if(isDown){
                XqLog.d(TAG,"下拉数据成功刷新界面");
                newsList.addAll(0,weChatNewsList);
                weChatNewsAdapter.addData(0,weChatNewsList);
                weChatNewsAdapter.notifyDataSetChanged();
            }else {
                XqLog.d(TAG,"上拉数据成功刷新界面");
                newsList.addAll(weChatNewsList); //添加到集合下面
                weChatNewsAdapter.addData(weChatNewsList);//刷新adapter
            }
        }

        if(ListUtils.isEmpty(newsList)){
            weChatNewsAdapter.setEmptyView(emptyView);
        }

    }


    @Override
    public void showError(String msg) {
        XqLog.d(TAG,"showError");
    }

    @Override
    public void showLoadingView(boolean isDown) {
        XqLog.d(TAG,"showLoadingView");
        if(isDown && !NetWorkUtil.isNetWorkAvailable(mActivity)){ //下拉等待时先判断一下当前的网络是否可用，不可用就停止下拉并显示网络不同的提示
            swipeRefreshLayout.setRefreshing(false);
            XqToast.showToastShortMsg(mActivity,getString(R.string.net_error_tip));
            return;
        }
        //当当前新闻列表为空时自动显示正在加载的等待动画
        weChatNewsAdapter.setEmptyView(R.layout.loading_flash_view, (ViewGroup) recyclerView.getParent());
    }

    @Override
    public void hideLoadingView(boolean isDown) {
        XqLog.d(TAG,"hideLoadingView");
        if(isDown){
            XqLog.d(TAG,"停止下拉数据");
           swipeRefreshLayout.setRefreshing(false);
        }else {
            XqLog.d(TAG,"停止上拉数据");
            weChatNewsAdapter.loadMoreComplete();
        }
        if(ListUtils.isEmpty(newsList)){ //当当前新闻列表为空时显示空白页
            weChatNewsAdapter.setEmptyView(emptyView);
        }
    }
}
